package com.mobilesalon.business;

import java.io.File;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import android.content.res.Resources;
import android.database.Cursor;

import com.mobilesalon.MobileSalonFactory;
import com.mobilesalon.R;
import com.mobilesalon.provider.DBHelper;
import com.mobilesalon.provider.ServiceClientContract.ServiceClient;
import com.mobilesalon.report.engine.ReportException;
import com.mobilesalon.report.engine.ReportExporter;
import com.mobilesalon.report.model.ReportBean;
import com.mobilesalon.tools.CursorToArray;
import com.mobilesalon.tools.DateTools;

public class ServiceClientReportManager {

	private static final String TAG = ServiceClientReportManager.class.getName();

	private final DBHelper dbHelper;

	private ServiceClientManager serviceClientManager;
	private final PreferencesManager prefsManager;
	private final ReportExporter reportExporter;

	public ServiceClientReportManager(DBHelper dbHelper, ServiceClientManager serviceClientManager,
			PreferencesManager prefsManager, ReportExporter reportExporter) {
		this.dbHelper = dbHelper;
		this.serviceClientManager = serviceClientManager;
		this.prefsManager = prefsManager;
		this.reportExporter = reportExporter;
	}

	public ReportBean fetchMonthAppointments(Calendar month) throws ConfigurationException {

		final Calendar beginMonth = DateTools.roundByMonth(month);
		final Calendar endMonth = Calendar.getInstance();
		endMonth.setTime(beginMonth.getTime());
		endMonth.add(Calendar.MONTH, 1);

		final String from = DateTools.TIMESTAMP_DATEFORMAT.format(beginMonth.getTime());
		final String to = DateTools.TIMESTAMP_DATEFORMAT.format(endMonth.getTime());
		final List<String> calendarOwners = prefsManager.getSelectedCalendarsOwners();

		Cursor cursor = dbHelper.reportMonthServiceClientGroupByService(from, to, calendarOwners);

		float sumCountService = 0;
		float sumAmountService = 0;
		while (cursor.moveToNext()) {
			sumAmountService = sumAmountService + cursor.getFloat(cursor.getColumnIndex("sum"));
			sumCountService = sumCountService + cursor.getFloat(cursor.getColumnIndex("count"));
		}
		cursor.move(-1);

		ReportBean report = new ReportBean(cursor, sumCountService, sumAmountService);
		return report;
	}

	public ReportBean fetchDetailledMonthAppointments(Calendar month) throws ConfigurationException {

		final Calendar beginMonth = DateTools.roundByMonth(month);
		final Calendar endMonth = Calendar.getInstance();
		endMonth.setTime(beginMonth.getTime());
		endMonth.add(Calendar.MONTH, 1);

		final String from = DateTools.TIMESTAMP_DATEFORMAT.format(beginMonth.getTime());
		final String to = DateTools.TIMESTAMP_DATEFORMAT.format(endMonth.getTime());
		final List<String> calendarOwners = prefsManager.getSelectedCalendarsOwners();

		Cursor cursor = dbHelper.reportMonthServiceClients(from, to, calendarOwners);

		String sd = CursorToArray.parseToString(cursor);
		float sumAmountService = 0;
		while (cursor.moveToNext()) {
			sumAmountService = sumAmountService + cursor.getFloat(cursor.getColumnIndex(ServiceClient.PRICE));
		}
		cursor.move(-1);

		ReportBean report = new ReportBean(cursor, 0, sumAmountService);
		return report;
	}

	public File exportMonthReport(ReportBean monthReportBean) throws ReportException {
		Resources res = MobileSalonFactory.getContext().getResources();

		String[] headerArray = res.getStringArray(R.array.service_month_report_detail_export_header);
		String[] footerArray = res.getStringArray(R.array.service_month_report_detail_export_footer);
		return reportExporter.generate(headerArray, monthReportBean.getMonthReportCursor(),
				footerArray);

	}

}
